package cn.hahah.module.blog.controller.admin.userlike;

import cn.hahah.module.blog.dal.dataobject.article.ArticleDO;
import cn.hahah.module.blog.dal.dataobject.users.UsersDO;
import cn.hahah.module.blog.service.article.ArticleService;
import cn.hahah.module.blog.service.users.UsersService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.hahah.framework.common.pojo.PageParam;
import cn.hahah.framework.common.pojo.PageResult;
import cn.hahah.framework.common.pojo.CommonResult;
import cn.hahah.framework.common.util.object.BeanUtils;
import static cn.hahah.framework.common.pojo.CommonResult.success;

import cn.hahah.framework.excel.core.util.ExcelUtils;

import cn.hahah.framework.apilog.core.annotation.ApiAccessLog;
import static cn.hahah.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.hahah.module.blog.controller.admin.userlike.vo.*;
import cn.hahah.module.blog.dal.dataobject.userlike.UserLikeDO;
import cn.hahah.module.blog.service.userlike.UserLikeService;

@Tag(name = "管理后台 - 用户点赞")
@RestController
@RequestMapping("/blog/user-like")
@Validated
public class UserLikeController {

    @Resource
    private UserLikeService userLikeService;
    @Resource
    private UsersService usersService;
    @Resource
    private ArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "创建用户点赞")
    @PreAuthorize("@ss.hasPermission('blog:user-like:create')")
    public CommonResult<Long> createUserLike(@Valid @RequestBody UserLikeSaveReqVO createReqVO) {
        return success(userLikeService.createUserLike(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户点赞")
    @PreAuthorize("@ss.hasPermission('blog:user-like:update')")
    public CommonResult<Boolean> updateUserLike(@Valid @RequestBody UserLikeSaveReqVO updateReqVO) {
        userLikeService.updateUserLike(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户点赞")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:user-like:delete')")
    public CommonResult<Boolean> deleteUserLike(@RequestParam("id") Long id) {
        userLikeService.deleteUserLike(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户点赞")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:user-like:query')")
    public CommonResult<UserLikeRespVO> getUserLike(@RequestParam("id") Long id) {
        UserLikeDO userLike = userLikeService.getUserLike(id);
        return success(BeanUtils.toBean(userLike, UserLikeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户点赞分页")
    @PreAuthorize("@ss.hasPermission('blog:user-like:query')")
    public CommonResult<PageResult<UserLikeRespVO>> getUserLikePage(@Valid UserLikePageReqVO pageReqVO) {
        PageResult<UserLikeDO> pageResult = userLikeService.getUserLikePage(pageReqVO);
        PageResult<UserLikeRespVO> result = BeanUtils.toBean(pageResult, UserLikeRespVO.class);
        for (UserLikeRespVO userLikeRespVO : result.getList()) {
            UsersDO usersDO = usersService.getUsers(userLikeRespVO.getUserId());
            ArticleDO articleDO = articleService.getArticle(userLikeRespVO.getArticleId());

            if( usersDO != null){
                userLikeRespVO.setUserName(usersDO.getUsername());
            }
            if( articleDO != null){
                userLikeRespVO.setArticleName(articleDO.getTitle());
            }
        }

        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户点赞 Excel")
    @PreAuthorize("@ss.hasPermission('blog:user-like:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserLikeExcel(@Valid UserLikePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<UserLikeDO> list = userLikeService.getUserLikePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户点赞.xls", "数据", UserLikeRespVO.class,
                        BeanUtils.toBean(list, UserLikeRespVO.class));
    }

}